package com.ynny.student.systemDB;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Component;

import javax.xml.transform.Source;
import java.util.List;

@Component("ScoreManage")
public class ScoreManageImpl implements ScoreManage{
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void addScore(Score score) {
        //定义SQL插入语句，实现插入一条学生成绩记录
        String sql ="insert into scores(number,Chinese,math,English) value(?,?,?,?)";

        Object[] obj =new Object[]{
                score.getNumber(),
                score.getChinese(),
                score.getMath(),
                score.getEnglish()
        };

        //执行SQL插入语句返回执行添加操作的记录条数
        int num = jdbcTemplate.update(sql,obj);
        if (num <= 0){

            System.out.println("插入学生成绩记录失败");
        }

    }

    @Override
    public void updateScore(String number, String course, Double score) {
        String sql = "update score set" + course + "=? where number=?";

        Object[] params = new Object[]{score, number};
        int num = jdbcTemplate.update(sql, params);
        if (num <= 0) {
            System.out.println("更新学生成绩记录失败");
        }

    }

    @Override
    public void queryScoreByName(String number) {
        String sql ="select * from score where number=?";
        //创建一个BeanPropertyRowMapper对象
        RowMapper<Score> rowMapper;
        rowMapper = new BeanPropertyRowMapper<Score>(Score.class);
        Score score = jdbcTemplate.queryForObject(sql, rowMapper, number);

        System.out.println(score);
    }



    @Override
    public void queryAllScore() {
        String sql = "select * from score";
        RowMapper<Score> rowMapper;
        rowMapper = new BeanPropertyRowMapper<Score>(Score.class);
        List<Score> scores = jdbcTemplate.query(sql,rowMapper);
        for (Score score:scores){
            System.out.println(score);
        }

    }
}
